//
//  LoginViewController.m
//  Socrates
//
//  Created by shy on 15/3/2.
//  Copyright (c) 2015年 shy. All rights reserved.
//

#import "LoginViewController.h"

#import "RSTAPIClient.h"
#import "MainViewController.h"
#import "RegisterViewController.h"
#import "ResetCodeViewController.h"
#import "AppDelegate.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "PresentationViewController.h"
#import "PreferViewController.h"
#import "MeViewController.h"
#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import "ResetCodeViewController.h"

@interface LoginViewController ()<UIGestureRecognizerDelegate, UIScrollViewDelegate>
@property (strong, nonatomic) UIView *topView;
@property (strong, nonatomic) UILabel *name;
@property (strong, nonatomic) UIButton *registerButton;
@property (strong, nonatomic) UIView *lineDown;
@property (strong, nonatomic) UIView *bgA;
@property (strong, nonatomic) UITextField *loginName;
@property (strong, nonatomic) UIView *bgB;
@property (strong, nonatomic) UITextField *code;
@property (strong, nonatomic) UIButton *forgetCode;
@property (strong, nonatomic) UIButton *login;

@property (strong, nonatomic) UIViewController *viewController;
@end

@implementation LoginViewController

#pragma mark -
#pragma mark init and dealloc
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
#pragma mark -
#pragma mark view life cycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = [NSString stringWithFormat:@"%@", NSLocalizedString(@"登录", nil)];
    
    self.view.backgroundColor = [UIColor colorWithRed:239/255. green:239/255. blue:244/255. alpha:1.0];
    [self.view addSubview:self.topView];
    
    [self.view addSubview:self.name];
    
    [self.view addSubview:self.registerButton];
    
    [self.view addSubview:self.lineDown];
    
    [self.view addSubview:self.bgA];
    
    [self.view addSubview:self.loginName];
    
    [self.view addSubview:self.bgB];
    
    [self.view addSubview:self.code];
    
    [self.view addSubview:self.forgetCode];
    
    [self.view addSubview:self.login];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //Notification监听
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}



#pragma mark - Custom Delegate
#pragma mark UpdateUserInfoDelegate
- (void)updateUserInfoResult:(NSNumber*)result {
    
}

#pragma mark AllSNSDetailViewControllerDelegate
#pragma END  - Custom Delegate



#pragma mark - Public Method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)loadMainView {
    [self setupViewControllers];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    nc.navigationBar.hidden = YES;
    [self customizeInterface];
    [self presentViewController:nc animated:NO completion:nil];
}
#pragma END - Public Method



#pragma mark - Rrivate Method
- (void)setupViewControllers {
    UIViewController *firstViewController = [[HomeViewController alloc] init];
    UIViewController *firstNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:firstViewController];
    
    UIViewController *secondViewController = [[MessageViewController alloc] init];
    UIViewController *secondNavigationController = [[UINavigationController alloc]
                                                    initWithRootViewController:secondViewController];
    
    UIViewController *thirdViewController = [[PresentationViewController alloc] init];
    UIViewController *thirdNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:thirdViewController];
    
    UIViewController *forthViewController = [[PreferViewController alloc] init];
    UIViewController *forthNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:forthViewController];
    
    UIViewController *fifthViewController = [[MeViewController alloc] init];
    UIViewController *fifthNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:fifthViewController];
    
    RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
    [tabBarController setViewControllers:@[firstNavigationController, secondNavigationController,
                                           thirdNavigationController, forthNavigationController, fifthNavigationController]];
    self.viewController = tabBarController;
    [self customizeTabBarForController:tabBarController];
    
}

- (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
    UIImage *finishedImage = [UIImage imageNamed:@"tabbar_selected_background"];
    UIImage *unfinishedImage = [UIImage imageNamed:@"tabbar_normal_background"];
    NSArray *tabBarItemImages = @[@"first", @"second", @"third", @"forth", @"fifth"];
    
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
        [item setBackgroundSelectedImage:finishedImage withUnselectedImage:unfinishedImage];
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",
                                                        [tabBarItemImages objectAtIndex:index]]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        
        index++;
    }
}

- (void)customizeInterface {
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    
    UIImage *backgroundImage = nil;
    NSDictionary *textAttributes = nil;
    
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];
        
        textAttributes = @{
                           NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
                           NSForegroundColorAttributeName: [UIColor blackColor],
                           };
    } else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
        backgroundImage = [UIImage imageNamed:@"navigationbar_background"];
        
        textAttributes = @{
                           UITextAttributeFont: [UIFont boldSystemFontOfSize:18],
                           UITextAttributeTextColor: [UIColor blackColor],
                           UITextAttributeTextShadowColor: [UIColor clearColor],
                           UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
                           };
#endif
    }
    
    [navigationBarAppearance setBackgroundImage:backgroundImage
                                  forBarMetrics:UIBarMetricsDefault];
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
    
}
#pragma END  - Rrivate Method



#pragma mark - Event Response
- (void)clickRegister {
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)forgetPwdButtonClicked {
    ResetCodeViewController *vc = [[ResetCodeViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)touchCompleteButton {
    [self loadMainView];
}
#pragma END - Event Response



#pragma mark -setter and getter
- (UIView *)topView{
    if (_topView ==nil) {
        UIImageView *backgroundImageView;
        backgroundImageView.tag = 10001;
        if ([UIScreen mainScreen].bounds.size.height*2 == [UIScreen mainScreen].bounds.size.width*3) {
            backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        } else {
            backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        }
        [backgroundImageView setImage:[[UIImage imageNamed:@"login"] stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
        [backgroundImageView setUserInteractionEnabled:NO];
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [_topView addSubview:backgroundImageView];
    }
    return _topView;
}
- (UILabel *)name{
    if (_name == nil) {
        _name = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 75)/2, 0.22*SCREEN_HEIGHT, 75, 32)];
        _name.font = [UIFont systemFontOfSize:32.];
        _name.text = @"登录";
        _name.textAlignment = NSTextAlignmentCenter;
        _name.textColor = [UIColor whiteColor];
    }
    return _name;
}

- (UIButton *)registerButton{
    if (_registerButton == nil) {
        _registerButton = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 75)/2, 0.32*SCREEN_HEIGHT, 75, 12)];
        [_registerButton setTitle:@"新用户注册" forState:UIControlStateNormal];
        _registerButton.titleLabel.font = [UIFont systemFontOfSize:12.];
        [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(clickRegister) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}

- (UIView *)lineDown{
    if (_lineDown == nil) {
        _lineDown = [[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 60)/2, 0.32*SCREEN_HEIGHT + 12, 60, 1)];
        _lineDown.backgroundColor = [UIColor colorWithRed:208/255. green:208/255. blue:208/255. alpha:1.0];
    }
    return _lineDown;
}

- (UIView *)bgA{
    if (_bgA == nil) {
        _bgA = [[UIView alloc] initWithFrame:CGRectMake(0, 0.44*SCREEN_HEIGHT, SCREEN_WIDTH, 42)];
        _bgA.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    }
    return _bgA;
}

- (UITextField *)loginName{
    if (_loginName == nil) {
        _loginName = [[UITextField alloc] initWithFrame:CGRectMake(0.14*SCREEN_WIDTH, 0.44*SCREEN_HEIGHT + 11, 0.758*SCREEN_WIDTH, 20)];
        [_loginName setPlaceholder:NSLocalizedString(@"$$$phone_number", nil)];
        [_loginName setValue:[UIColor colorWithRed:207/255. green:207/255. blue:207/255. alpha:1.0]forKeyPath:@"_placeholderLabel.textColor"];
        _loginName.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _loginName;
}

- (UIView *)bgB{
    if (_bgB == nil) {
        _bgB = [[UIView alloc] initWithFrame:CGRectMake(0, 0.44*SCREEN_HEIGHT + 72, SCREEN_WIDTH, 42)];
        _bgB.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    }
    return _bgB;
}

- (UITextField *)code{
    if (_code == nil) {
        _code = [[UITextField alloc] initWithFrame:CGRectMake(0.14*SCREEN_WIDTH, 0.44*SCREEN_HEIGHT + 83, 0.758*SCREEN_WIDTH, 20)];
        [_code setPlaceholder:NSLocalizedString(@"$$$login_code", nil)];
        [_code setValue:[UIColor colorWithRed:207/255. green:207/255. blue:207/255. alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
        _code.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _code;
}

- (UIButton *)forgetCode{
    if (_forgetCode == nil) {
        _forgetCode = [[UIButton alloc] initWithFrame:CGRectMake(0.7*SCREEN_WIDTH, 0.44*SCREEN_HEIGHT + 124, 0.25*SCREEN_WIDTH, 12.)];
        _forgetCode.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_forgetCode setTitle:NSLocalizedString(@"$$$forget_code", nil) forState:UIControlStateNormal];
        [_forgetCode.titleLabel setFont:[UIFont systemFontOfSize:12.]];
        [_forgetCode setTitleColor:[UIColor colorWithRed:30/255. green:144/255. blue:255/255. alpha:1.] forState:UIControlStateNormal];
        [_forgetCode addTarget:self action:@selector(forgetPwdButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetCode;
}

- (UIButton *)login{
    if (_login == nil) {
        _login = [[UIButton alloc] initWithFrame:CGRectMake(0.27*SCREEN_WIDTH, 0.74*SCREEN_HEIGHT, 0.46*SCREEN_WIDTH, 0.11*SCREEN_WIDTH)];
        [_login setTitle:NSLocalizedString(@"$$$login", nil) forState:UIControlStateNormal];
        [_login setBackgroundColor:[UIColor colorWithRed:224/255. green:102/255. blue:102/255. alpha:1.]];
        [_login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_login addTarget:self action:@selector(touchCompleteButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _login;
}
@end



